<?php
declare (strict_types = 1);

namespace app\attachment\controller;
use think\facade\Env;
class Image extends Base
{
    private $allow_extension = ['jpeg', 'jpe', 'jpg', 'gif', 'png', 'webp'];
    private $addition_water = false;
    // protected $file_system;
    protected function initialize()
    {
        parent::initialize();
    }
    public function single(){
        // 获取表单上传文件
        $file = request()->file('file');
        $hash = $file->hash('sha1');
        $attachment = $this->model->where('hash',$hash)->find();
        if($attachment !== null){
            return json($attachment);
        } else {
            $module = request()->param('module', 'common');
            // 验证上传内容
            try {
                // validate(['image'=>'filesize:10240|fileExt:jpg|image:200,200,jpg'])->check([$file]);
                // 上传到本地服务器
                $savename =  \think\facade\Filesystem::putFile($module, $file);
            } catch (think\exception\ValidateException $e) {
                return json($e->getMessage(),500);
            }
            $url =  \think\facade\Filesystem::getConfig('disks.public.url');

            $attachment = [];
            $attachment['filename'] = $file->getOriginalName();
            $attachment['filepath'] = $savename;
            $attachment['filesize'] = $file->getSize();
            $attachment['fileext'] = $file->getOriginalExtension();
            $attachment['uploader_id'] = request()->uid;
            $attachment['is_cover'] = false;
            $attachment['water'] = false;
            $attachment['url'] = $attachment['location'] =$this->get_server_url($url.'/'.$savename);
            $attachment['hash'] = $file->hash('sha1');
            $attachment['module'] = $module;
            if($this->save_log){
                $this->model->save($attachment);
            }
            return json($attachment);
        }
    }
    public function multiple(){
    }
    // 单图上传
    // public function single(){
    //     // 获取表单上传文件 例如上传了001.jpg
    //     $file = $this->request->file('file');
    //     $water = $this->request->param('water', 'true');
    //     // 移动到框架应用根目录/uploads/ 目录下
    //     if($file){
    //         $info = $file->move('.'.DIRECTORY_SEPARATOR.$this->upload_path);
    //         $ext = $info->getExtension();
    //         if(!in_array($ext,$this->allow_extension)){
    //             return json('错误的文件类型',500);
    //         }
    //     }else{
    //         return json('文件不存在',500);
    //     }
    //     if($this->addition_water || $water === 'true'){
    //         $file_path = Env::get('root_path').DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.$this->upload_path.DIRECTORY_SEPARATOR.$info->getSaveName();
    //         $image = \think\Image::open($file_path);
    //         $image->water(Env::get('APP_PATH').'/attachment/water.png',\think\Image::WATER_SOUTHEAST,50)->save(str_replace('.','ab2_water.',$file_path)); 
    //     }
    //     if($info){
    //         $attachment = [];
    //         $attachment['filename'] = $info->getInfo('name');
    //         if($this->addition_water || $water === 'true'){
    //             $attachment['filepath'] = "/".$this->upload_path."/".str_replace('.','ab2_water.',$info->getSaveName());
    //         }else {
    //             $attachment['filepath'] = "/".$this->upload_path."/".$info->getSaveName();
    //         }
    //         $attachment['filesize'] = $info->getInfo('size');
    //         $attachment['fileext'] = $info->getExtension();
    //         $attachment['uploader_id'] = $this->userID;
    //         $attachment['is_cover'] = false;
    //         $attachment['water'] = $water;
    //         $attachment['url'] = "http://".$_SERVER['HTTP_HOST'].$attachment['filepath'];
    //         $attachment['location'] = "http://".$_SERVER['HTTP_HOST'].$attachment['filepath'];
    //         if($this->save_log){
    //             $this->model->save($attachment);
    //         }
    //         return json($attachment);
    //     }else{
    //         // 上传失败获取错误信息
    //         return json($file->getError(),500);
    //     }
    // }
    // public function multiple(){
    //     // $this->setAllowMethod(['post']);
    //     $files = $this->request->file('images');
    //     $response = [];
    //     foreach($files as $file){
    //         // 移动到框架应用根目录/uploads/ 目录下
    //         $info = $file->move( '.'.DIRECTORY_SEPARATOR.$this->upload_path);
    //         if($info){
    //             $attachment = [];
    //             $attachment['filename'] = $info->getInfo('name');
    //             $attachment['filepath'] = "/".$this->upload_path."/".$info->getSaveName();
    //             $attachment['filesize'] = $info->getInfo('size');
    //             $attachment['fileext'] = $info->getExtension();
    //             $attachment['uploader_id'] = null;
    //             $attachment['url'] = $this->get_server_url($attachment['filepath']);
    //             $response[] = $attachment;
    //         }else{
    //             // 上传失败获取错误信息
    //             return json($info->getError(),500);
    //         }    
    //     }
    //     if($this->save_log){
    //         $this->model->saveAll($response);
    //     }
    //     return json($response);
    // }
}
